import {
	mapState
} from 'vuex'
import store from "@/store"
import {
	baseUrl
} from '@/config/config.js'
import {
	isWeiXin
} from "../common/helper";
// #ifdef H5
import jWeixin from 'jweixin-module'
// #endif

let $uStoreKey = [];
try {
	$uStoreKey = store.state ? Object.keys(store.state) : [];
} catch (e) {

}

module.exports = {
	created() {
		this.$u.vuex = (name, value) => {
			this.$store.commit('$uStore', {
				name,
				value
			})
		}
	},
	computed: {
		...mapState($uStoreKey),
		addressUrl() {
			return baseUrl + '/api/get_areas'
		},
		isLogin() {
			return this.user_id > 0
		}
	},
	methods: {
		officialConfig(callback) {
			//#ifdef H5
			if (isWeiXin()) {
				this.$post('api/offical-config', {
					url: window.location.origin + window.location.pathname,
					apis: ['getLocation', 'updateAppMessageShareData', 'updateTimelineShareData',
						'chooseWXPay', 'scanQRCode'
					],
					debug: false,
				}).then(res => {
					console.info({
						res
					})
					jWeixin.config(res);
					jWeixin.ready(() => {
						callback && callback()

						jWeixin.updateAppMessageShareData({
							title: this.vuex_config.store_title,
							desc: this.vuex_config.store_title,
							link: window.location.origin,
							imgUrl: this.vuex_config.wap_login_logo,

							success: function() {
								// 设置成功
							}
						})

						jWeixin.updateTimelineShareData({
							title: this.vuex_config.store_title,
							link: window.location.origin,
							imgUrl: this.vuex_config.wap_login_logo,
							success: function() {
								// 设置成功
							}
						})
					});
					jWeixin.error(function(res) {
						console.log("接口调取失败", res)
					});
				})
			}
			//#endif
		}
	}
}